package com.lgywin.runserver.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgywin.runserver.comm.Cache;
import com.lgywin.runserver.dao.GameServerMapper;
import com.lgywin.runserver.dao.UserMapper;
import com.lgywin.runserver.entity.GameServer;
import com.lgywin.runserver.entity.User;
import com.lgywin.runserver.entity.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class UserController {
    @Value("${netty_port}")
    private int port;
    @Value("${netty_ip}")
    private String ip;
    @Autowired
    private UserMapper userDao;
    @Autowired
    private GameServerMapper gameServerDao;

    @ResponseBody
    @RequestMapping("/serverState") //这里为空或者是/都能进入该方法
    public GameServer serverState()  {
        if(Cache.GameServer == null || Cache.GameServer.getId() == null){
            GameServer condition = new GameServer();
            condition.setIp(ip);
            condition.setPort(port);
            GameServer gameServer = gameServerDao.find(condition);
            if(gameServer!=null && gameServer.getId()!=null){
                Cache.GameServer.setId(gameServer.getId());
                Cache.GameServer.setName(gameServer.getName());
                //TODO
                Cache.GameServer.setState(1);

                Cache.GameServer.setIp(gameServer.getIp());
                Cache.GameServer.setPort(gameServer.getPort());
                Cache.GameServer.setPort_http(gameServer.getPort_http());
                Cache.GameServer.setAddress(gameServer.getAddress());
            }
        }
        return Cache.GameServer;
    }

    @GetMapping("/")
    public String index(Model model, UserQuery userQuery){
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
        PageInfo<User> userPageInfo = new PageInfo<User>(userDao.select(userQuery));
        model.addAttribute("page",userPageInfo);
        return "index";
    }

    @PostMapping("/")
    public String listUserByName(Model model,UserQuery userQuery){
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
        PageInfo<User> userPageInfo = new PageInfo<User>(userDao.select(userQuery));
        model.addAttribute("page",userPageInfo);
        model.addAttribute("userQuery",userQuery);
        return "index";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id, RedirectAttributes attributes){
        int b = userDao.delete(id);
        if(b>0){
            attributes.addFlashAttribute("message","删除用户成功");
            return "redirect:/";
        }else {
            attributes.addFlashAttribute("message","删除用户失败");
            return "redirect:/";
        }
    }


    @GetMapping("/edit/{id}")
   public String toEdit(@PathVariable Integer id,Model model){
        model.addAttribute("user",userDao.getId(id));
        return "editUser";
   }


   @PostMapping("/edit")
   public String edit(User user,RedirectAttributes attributes){
       UserQuery userQuery = new UserQuery();
       Integer id = user.getId();
       userQuery.setUsername(user.getUsername());
       PageInfo<User> userPageInfo = new PageInfo(userDao.select(userQuery));
        if(id != null){
            if(userPageInfo.getSize() == 0){
                int b = userDao.update(user);
                if(b>0){
                    attributes.addFlashAttribute("message"," 更新用户成功");
                    return "redirect:/";
                }else {
                    attributes.addFlashAttribute("message","更新用户失败");
                    return "redirect:/";
                }
            }else {
                attributes.addFlashAttribute("message","该用户名已存在");
                return "redirect:/edit/"+user.getId();
            }
        }else {
            if(userPageInfo.getSize() == 0){
                int b = userDao.insert(user);
                if(b>0){
                    attributes.addFlashAttribute("message"," 新增用户成功");
                    return "redirect:/";
                }else {
                    attributes.addFlashAttribute("message","新增用户失败");
                    return "redirect:/";
                }
            }else {
                attributes.addFlashAttribute("message","该用户名已存在");
                return "redirect:/";
            }
        }
   }

   @GetMapping("/update")
   public String toUpdate(Model model){
       User user = new User();
       model.addAttribute("user",user);
       return "editUser";
   }

}
